package com.yn.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yn.pet.bus.dao.GenFenceAlarmDao;
import com.yn.pet.bus.entity.AlarmRecord;
import com.yn.pet.bus.entity.GenFenceAlarm;
import com.yn.pet.service.GenFenceAlarmService;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 地理围栏告警记录表(GenFenceAlarm)表服务实现类
 *
 * @author makejava
 * @since 2025-07-09 12:00:47
 */
@Service("genFenceAlarmService")
public class GenFenceAlarmServiceImpl extends ServiceImpl<GenFenceAlarmDao, GenFenceAlarm> implements GenFenceAlarmService {

    /**
     * 获取设备围栏告警记录
     * @param deviceId 设备id
     * @param startTime 开始时间区间
     * @param endTime 结束时间区间
     * @return 告警记录数量
     */
    @Override
    public Long getCountByDeviceId(Long deviceId,Date startTime, Date endTime) {
        if(deviceId == null){
            return null;
        }
        LambdaQueryWrapper<GenFenceAlarm> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(GenFenceAlarm::getDeviceId, deviceId)
                .between(GenFenceAlarm::getCreateTime, startTime, endTime);
        return this.count(lambdaQuery);
    }

    /**
     * 根据围栏id获取该围栏的最新一条的告警记录
     * @param fenceId 围栏id
     * @return 告警记录
     */
    @Override
    public GenFenceAlarm getLastFenceAlarmRecordByFenceId(Long fenceId) {
        if(fenceId==null){
            return null;
        }
        LambdaQueryWrapper<GenFenceAlarm> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(GenFenceAlarm::getFenceId, fenceId)
                .orderByDesc(GenFenceAlarm::getCreateTime)
                .last("limit 1");
        return this.getOne(lambdaQuery);
    }

}

